package com.pn.warehouse.service.impl;

import com.pn.warehouse.entity.ProductType;
import com.pn.warehouse.entity.Result;
import com.pn.warehouse.mapper.ProductTypeMapper;
import com.pn.warehouse.service.ProductTypeService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@CacheConfig(cacheNames = "com.pn.warehouse.service.impl.ProductTypeServiceImpl")
@Service
public class ProductTypeServiceImpl implements ProductTypeService {

    @Resource
    private ProductTypeMapper productTypeMapper;

    //查询所有的商品分类，并转成分类树
    @Cacheable(key = "'all:typeTree'")
    @Override
    public List<ProductType> productTypeTree() {
        //1、查出所有的商品分类
        List<ProductType> allProductType = productTypeMapper.findAllProductType();
        //2、将所有商品分类转换成商品分类树，并返回
        List<ProductType> productTypeTree = allTypeToTypeTree(allProductType, 0);
        return productTypeTree;
    }

    //将所有商品分类转换成商品分类树
    public List<ProductType> allTypeToTypeTree(List<ProductType> typeList, Integer pid){
        //拿到所有的一级分类
        List<ProductType> firstType = new ArrayList<>();
        for (ProductType productType : typeList) {
            if (productType.getParentId().equals(pid)){
                firstType.add(productType);
            }
        }

        //拿到所有的一级分类下的二级分类
        for (ProductType productType : firstType) {
            List<ProductType> secondType = allTypeToTypeTree(typeList, productType.getTypeId());
            productType.setChildProductCategory(secondType);
        }

        return firstType;
    }

    //检查分类编码是否已经存在
    @Override
    public Result findTypeByCodeOrName(String typeCode) {
        //1、新建一个productType对象封装typeCode
        ProductType productType = new ProductType();
        productType.setTypeCode(typeCode);

        //2、查询并返回
        ProductType prod = productTypeMapper.findTypeByCodeOrName(productType);
        return Result.ok(prod == null);
    }

    //添加商品的分类
    @CacheEvict(key = "'all:typeTree'")
    @Override
    public Result insertProductType(ProductType productType) {
        //1、新建一个prodType封装名字
        ProductType prodType = new ProductType();
        prodType.setTypeName(productType.getTypeName());

        //2、判断该名字是否已经存在
        ProductType prod = productTypeMapper.findTypeByCodeOrName(productType);
        if (prod != null){
            return Result.err(Result.CODE_ERR_BUSINESS, "分类名称已存在！");
        }

        //3、插入分类，并返回
        int i = productTypeMapper.insertProductType(productType);
        if (i > 0){
            return Result.ok("添加分类成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "添加分类失败！");
    }

    //根据id或父级id删除分类
    @CacheEvict(key = "'all:typeTree'")
    @Override
    public Result removeProductType(Integer typeId) {
        int i = productTypeMapper.removeProductType(typeId);
        if (i > 0){
            return Result.ok("删除分类成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "删除分类失败！");
    }

    //根据id修改分类
    @CacheEvict(key = "'all:typeTree'")
    @Override
    public Result setProductTypeById(ProductType productType) {
        //1、新建一个prodType封装名字
        ProductType prodType = new ProductType();
        prodType.setTypeName(productType.getTypeName());

        //2、判断该名字是否已经存在
        ProductType prod = productTypeMapper.findTypeByCodeOrName(prodType);
        if (prod != null && !prod.getTypeId().equals(productType.getTypeId())){ //该名字的商品存在，并且不是当前的商品
            return Result.err(Result.CODE_ERR_BUSINESS, "商品名字已存在！");
        }

        //3、修改分类，并返回
        int i = productTypeMapper.setProductTypeById(productType);
        if (i > 0){
            return Result.ok("修改分类成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "修改分类失败！");
    }

}

